/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trocabi.Socle;

/**
 *
 * @author philippe
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;

public class JDateTextField extends JFormattedTextField {
    private SimpleDateFormat dateFormat=null;
    private Date             date=null;
    
    public class FormattedTextFieldVerifier extends InputVerifier {
        @Override
        public boolean verify(JComponent input) {
            if (input instanceof JFormattedTextField) {
                Color black=new Color(0,0,0);  
                input.setForeground(black);  
                input.setToolTipText(null);
                JFormattedTextField ftf = (JFormattedTextField) input;
                AbstractFormatter formatter = ftf.getFormatter();
                 if (formatter != null) {
                    String text = ftf.getText();
                    try {
                        date = dateFormat.parse(text);
                        return true;
                    } catch (ParseException e) {
                        Color red=new Color(255,0,0);  
                        input.setForeground(red);    
                        input.setToolTipText("La date est invalide");
                        return false;
                    }
                }
            }
            return true;
        }

        @Override
        public boolean shouldYieldFocus(JComponent input) {
            return verify(input);
        }
    }
    
    public JDateTextField(String _dateFormat) throws ParseException {
        super(new MaskFormatter(_dateFormat.replaceAll("[GyMwWDdFEaHkKhmsSzZ]", "#"))); // transform dateFormat into mask dd/MM/yy -> ##/##/##
        this.dateFormat=new SimpleDateFormat(_dateFormat);
        this.dateFormat.setLenient(false); // disable "modulo on d
        MaskFormatter maskDate = (MaskFormatter) this.getFormatter();
        maskDate.setValidCharacters("0123456789");
        this.setInputVerifier(new FormattedTextFieldVerifier());
    }
    
    public JDateTextField(String _dateFormat, Date _date) throws ParseException {
        this(_dateFormat);//JDateTextField();
        this.date=_date;
        this.setText(dateFormat.format(_date));
    }
    
    public Date getDate() { 
        return date;
    }
    
    public void setDate(Date _date) {
        date=_date;
        this.setText(dateFormat.format(date));
    }
    /**
     * Sample usage
     * @param args
     * @throws ParseException 
     */
    public static void main(final String args[]) throws ParseException {
        JFrame frame = new JFrame("Formatted Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JDateTextField myJDateTExteField = new JDateTextField("dd/MM/yy",new Date());
        JTextField simpleText=new JTextField();
        frame.add(myJDateTExteField, BorderLayout.NORTH);
        frame.add(simpleText, BorderLayout.SOUTH);
        frame.setSize(250, 100);
        frame.setVisible(true);
    }
}